package com.springboot2.shiro.comm.entity;

/**
 * RespResult
 *
 * @author Archx[archx@foxmail.com]
 * at 2018/7/13 22:19
 */

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class RespResult implements Serializable {
    private static final long serialVersionUID = -1001914987883738904L;

    /**
     * code 0: 正常 1:失败 -1:异常
     */
    private int                 code;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String              message;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Object              result;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Map<String, Object> data;
    private Long                timestamp;

    public RespResult(RespResult.Builder builder) {
        this.code = builder.code;
        this.message = builder.message;
        this.result = builder.result;
        this.data = builder.data;
        this.timestamp = Calendar.getInstance().getTimeInMillis();
    }

    public static Builder builder() {
        return new Builder();
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public Object getResult() {
        return this.result;
    }

    public Map<String, Object> getData() {
        return this.data;
    }

    public Long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public String toString() {
        return JSON.toJSONString(this);
    }

    public static class Builder {
        private int                 code;
        private String              message;
        private Object              result;
        private Map<String, Object> data;

        public Builder() {
        }

        public RespResult.Builder code(RespResult.Code code) {
            this.code = code.code();
            return this;
        }

        public RespResult.Builder code(int code) {
            this.code = code;
            return this;
        }

        public RespResult.Builder message(String message) {
            this.message = message;
            return this;
        }

        public RespResult.Builder result(Object result) {
            this.result = result;
            return this;
        }

        public RespResult.Builder data(String key, Object value) {
            if (this.data == null) {
                this.data = new HashMap<>();
            }

            this.data.put(key, value);
            return this;
        }

        public RespResult.Builder data(Map<String, Object> map) {
            if (this.data == null) {
                this.data = new HashMap<>();
            }

            this.data.putAll(map);
            return this;
        }

        public RespResult build() {
            return new RespResult(this);
        }

        public RespResult build(boolean condition) {
            if (condition) {
                this.code(RespResult.Code.Success);
            } else {
                this.code(RespResult.Code.Failure);
            }

            return new RespResult(this);
        }
    }

    public enum Code {
        Success(0), Failure(1), Exception(-1);

        private final int code;

        private Code(int code) {
            this.code = code;
        }

        public int code() {
            return this.code;
        }
    }
}
